package com.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Document;
import com.entity.StoreHouse;
import com.entity.Team;
import com.entity.User;
import com.entity.base.Result;
import com.mapper.TeamMapper;
import com.service.DocumentService;
import com.service.StoreHouseService;
import com.service.TeamService;
import com.service.UserService;
import com.service.oss.OSSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping(value = "/teacher")
@Slf4j
@Api(tags = "教师相关接口", description = "提供相关的 Rest API")
public class TeacherController {

    @Autowired
    OSSClient ossClient;

    @Autowired
    TeamService teamService;

    @Autowired
    StoreHouseService storeHouseService;

    @Autowired
    DocumentService documentService;

    @ApiOperation("创建资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "team", value = "班级实体", required = true, dataType = "user", paramType = "body")
    })
    @PostMapping("/createStoreHouse")
    public Result createStoreHouse(@RequestBody Team team){
        try {
            if (team.getStoreHouseId()>0) return Result.error("该班级已有资源库");
            StoreHouse storeHouse = new StoreHouse();
            storeHouse.setId(System.currentTimeMillis());
            storeHouse.setStatus(1);
            storeHouse.setTCapacity(100.0*1024);//100M存储空间
            storeHouse.setUCapacity(0.0);
            storeHouse.setRCapacity(100.0*1024);
            storeHouse.setRoleId(1);
            ossClient.mkDir(storeHouse.getId().toString()+"/");
            team.setStoreHouseId(storeHouse.getId());
            teamService.updateById(team);
            if(storeHouseService.save(storeHouse)) return Result.success(storeHouse);
            return Result.error("创建失败");
        }catch (Exception e){
            log.error(e.toString());
            return Result.error(e.toString());
        }
    }
//    @ApiOperation("删除资源库")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "storeHouse", value = "资源库", required = true, dataType = "user", paramType = "body")
//    })
//    @PostMapping("/deleteStoreHouse")
//    public Result deleteStoreHouse(@RequestBody StoreHouse storeHouse){
//        if (storeHouse.getRoleId()!=1) return Result.error("无权限");
//        String path = System.getProperty("user.dir")+storeHouse.getPath();
//        if (documentService.deleteFolder(path)) {
//            storeHouseService.removeById(storeHouse.getId());
//            QueryWrapper<Team> wrapper = new QueryWrapper<>();
//            wrapper.eq("store_house_id",storeHouse.getId());
//            Team team = teamService.getOne(wrapper);
//            team.setStoreHouseId(null);
//            teamService.updateById(team);
//            return Result.success();
//        }
//        return Result.error("删除失败");
//    }
}
